package com.car.action.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.compress.utils.Lists;

import com.car.common.model.Banner;
import com.car.common.model.Prop;
import com.car.common.model.PropRecord;
import com.car.common.model.Social;
import com.car.common.model.Users;
import com.jfinal.core.Controller;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.utils.APIJSON;
import com.utils.StringUtil;

public class BannersController extends Controller {

	public void list() {
		/*
		ArrayList<Banner> bannerList = CacheKit.get("constant", "bannerlist", new IDataLoader(){
		       public Object load() {
		           return ;
		}});
		*/
		renderText(APIJSON.send(Banner.dao.getAll()));
	}
	
	
	public void consume_list() {
		List<PropRecord> recordList = PropRecord.dao.find("SELECT sum(hot_val) AS hot_val, user_id FROM prop_record WHERE user_id > 1000000 and user_id != 1000039 GROUP BY user_id HAVING hot_val > 0 ORDER BY hot_val DESC limit 20");
		List<Map<String,Object>> result = Lists.newArrayList();
		for(PropRecord one: recordList) {
			String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit  from users as u left join area ae on ae.code = u.area_id where u.id = " + one.get("user_id");
			Users user1 = Users.dao.findFirst(sql);
			Map<String,Object> user_info = Users.dao.info(user1,null);
			user_info.put("hot_val", one.get("hot_val"));
			result.add(user_info);
		}
		if (recordList.size() < 20 ) {
			List<Users> usersList = Users.dao.find("select id, consume_hot_val,hot_val from users where sex = 1 order by consume_hot_val desc,hot_val desc limit " + (20 - recordList.size()));
			for(Users user: usersList) {
				String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
				sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit  from users as u left join area ae on ae.code = u.area_id where u.id = " + user.get("id");
				Users user1 = Users.dao.findFirst(sql);
				Map<String,Object> user_info = Users.dao.info(user1,null);
				user_info.put("hot_val", user.get("hot_val"));
				result.add(user_info);
			}
		}
		
		renderText(APIJSON.send(result));
	}
	
	
	/**
	 * 魅力榜
	 */
	public void charm_list() {
		List<PropRecord> recordList = PropRecord.dao.find("SELECT sum(hot_val) AS hot_val,target_user_id FROM prop_record where target_user_id != 141 and target_user_id != 200 and target_user_id != 140901 GROUP BY target_user_id  HAVING hot_val > 0 ORDER BY hot_val DESC limit 20 ");
		List<Map<String,Object>> result = Lists.newArrayList();
		for(PropRecord one: recordList) {
			String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit  from users as u left join area ae on ae.code = u.area_id where u.id = " + one.get("target_user_id");
			Users user1 = Users.dao.findFirst(sql);
			Map<String,Object> user_info = Users.dao.info(user1,null);
			user_info.put("charm", one.get("hot_val"));
			result.add(user_info);
		}
		if (recordList.size() < 20 ) {
			List<Users> usersList = Users.dao.find("select id, credit,hot_val from users where sex = 2 and id != 141 order by credit desc,hot_val desc limit " + (20 - recordList.size()));
			
			for(Users user: usersList) {
				String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
				sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit  from users as u left join area ae on ae.code = u.area_id where u.id = " + user.get("id");
				Users user1 = Users.dao.findFirst(sql);
				Map<String,Object> user_info = Users.dao.info(user1,null);
				user_info.put("charm", user.get("credit"));
				result.add(user_info);
			}
		}
		renderText(APIJSON.send(result));
	}
	
}
